<template>
  <div>
    <n-card :bordered="false" class="proCard">
      <n-form
        :label-width="200"
        :model="formValue"
        :rules="rules"
        label-placement="left"
        ref="formRef"
        class="py-8"
      >
        <n-grid cols="2" responsive="screen">
          <n-grid-item offset="0">
            <n-form-item label="劳务公司名称" path="name">
              <n-input v-model:value="formValue.name" placeholder="劳务公司名称" />
            </n-form-item>
            <n-form-item label="开户银行" path="bank">
              <n-input placeholder="开户银行" v-model:value="formValue.bank" />
            </n-form-item>
            <n-form-item label="劳务合同名称" path="contract">
              <n-input v-model:value="formValue.contract" placeholder="劳务合同名称" />
            </n-form-item>
            <n-form-item label="合同金额" path="price">
              <n-input-number
                v-model:value="formValue.price"
                clearable
                :precision="2"
                style="width: 100%"
              />
            </n-form-item>
            <n-form-item label="开工日期" path="start_date">
              <n-date-picker type="date" v-model:value="formValue.start_date" style="width: 100%" />
            </n-form-item>
            <n-form-item label="工期" path="duration">
              <n-input v-model:value="formValue.duration" placeholder="工期" />
            </n-form-item>
            <n-form-item label="服务费比例" path="service_fee_ratio">
              <n-input v-model:value="formValue.service_fee_ratio" placeholder="服务费比例" />
            </n-form-item>
            <n-form-item label="合同份数" path="contract_num">
              <n-input v-model:value="formValue.contract_num" placeholder="合同份数" />
            </n-form-item>
          </n-grid-item>
          <n-grid-item offset="0">
            <n-form-item label="劳务公司联系方式" path="contact">
              <n-input v-model:value="formValue.contact" placeholder="劳务公司联系方式" />
            </n-form-item>
            <n-form-item label="银行账号" path="bank_number">
              <n-input placeholder="银行账号" v-model:value="formValue.bank_number" />
            </n-form-item>
            <n-form-item label="合同类型" path="contract_type">
              <n-input placeholder="合同类型" v-model:value="formValue.contract_type" />
            </n-form-item>
            <n-form-item label="履约保证金" path="earnest_money">
              <n-input placeholder="履约保证金" v-model:value="formValue.earnest_money" />
            </n-form-item>
            <n-form-item label="竣工日期" path="end_date">
              <n-date-picker type="date" v-model:value="formValue.end_date" style="width: 100%" />
            </n-form-item>
            <n-form-item label="保修期" path="warranty_period">
              <n-input v-model:value="formValue.warranty_period" placeholder="保修期" />
            </n-form-item>
            <n-form-item label="承包方式" path="contracting_method">
              <n-input v-model:value="formValue.contracting_method" placeholder="承包方式" />
            </n-form-item>
            <n-form-item label="支付币种" path="payment_currency">
              <n-input v-model:value="formValue.payment_currency" placeholder="支付币种" />
            </n-form-item>
          </n-grid-item>
        </n-grid>
        <n-form-item label="施工内容" path="construction_content">
          <n-input
            v-model:value="formValue.construction_content"
            placeholder="施工内容"
            type="textarea"
          />
        </n-form-item>
        <n-form-item label="付款方式" path="pay_method">
          <n-input v-model:value="formValue.pay_method" placeholder="付款方式" type="textarea" />
        </n-form-item>
        <n-form-item label="备注" path="remarks">
          <n-input v-model:value="formValue.remarks" placeholder="备注" type="textarea" />
        </n-form-item>
        <n-form-item label="附件上传" path="files">
          <uploadFiles
            action="https://example.com/upload"
            accept="image/*"
            multiple
            :limit="5"
            :maxFileSize="5"
            placeholder="点击或拖拽图片到此处上传（最多5张，每张不超过5MB）"
            v-model="formValue.files"
          />
        </n-form-item>
      </n-form>
    </n-card>
    <n-space justify="center">
      <n-button type="warning" @click="formSubmit">存草稿</n-button>
      <n-button type="primary" @click="formSubmit">提交</n-button>
      <n-button @click="resetForm">重置</n-button>
    </n-space>
  </div>
</template>

<script lang="ts" setup>
  import { ref, unref, reactive, onMounted } from 'vue';
  import { useMessage } from 'naive-ui';
  import { useRouter } from 'vue-router';
  import { ResultEnum } from '@/enums/httpEnum';
  import {
    getLaborCompanyContract,
    patchLaborCompanyContract,
    putLaborCompanyContract,
  } from '@/api/laborCompany/companyContract';
  import uploadFiles from '@/components/uploadFiles.vue';

  const id: any = ref(useRouter().currentRoute.value.params.id);
  const rules = {
    name: {
      required: true,
      message: '请输入劳务公司名称',
      trigger: 'blur',
    },
    contract: {
      required: true,
      message: '请输入劳务合同名称',
      trigger: 'blur',
    },
    price: {
      required: true,
      message: '请输入合同金额',
      type: 'number',
      trigger: ['input', 'blur'],
    },
    contract_type: {
      required: true,
      message: '请选择合同类型',
      trigger: ['blur'],
    },
  };

  const formRef: any = ref(null);
  const message = useMessage();

  const defaultValueRef = () => ({
    id: 0,
    name: '',
    bank: '',
    contract: '',
    price: 0,
    start_date: null,
    duration: '',
    service_fee_ratio: '',
    contract_num: '',
    contact: '',
    bank_number: '',
    contract_type: '',
    earnest_money: '',
    end_date: null,
    warranty_period: '',
    contracting_method: '',
    payment_currency: '',
    construction_content: '',
    pay_method: '',
    remarks: '',
    files: [],
  });

  let formValue = reactive(defaultValueRef());
  function formSubmit() {
    formRef.value.validate(async (errors) => {
      if (!errors) {
        let response: any = null;
        if (formValue.id == 0) {
          response = await putLaborCompanyContract(formValue);
        } else {
          response = await patchLaborCompanyContract(formValue);
        }

        if (response.code == ResultEnum.SUCCESS) {
          message.success('操作成功');
          formValue.id = response.data;
          getData();
        }
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  const getData = async () => {
    const response: any = await getLaborCompanyContract(formValue.id);
    if (response.code === ResultEnum.SUCCESS) {
      response.data.price = parseFloat(response.data.price);
      Object.assign(formValue, response.data);
    }
  };

  function resetForm() {
    formRef.value.restoreValidation();
    formValue = Object.assign(unref(formValue), defaultValueRef());
  }

  onMounted(() => {
    formValue.id = id.value;
    if (formValue.id != 0) {
      getData();
    }
  });
</script>
